
#include <QtGui>

#include "FindDialog.h"

CFindDialog::CFindDialog(QWidget *parent)
: QDialog(parent)
{
    _pLabel = new QLabel(tr("Find &what:"),this);
    _pLineEdit = new QLineEdit(this);
    _pLabel->setBuddy(_pLineEdit);

    _pCaseCheckBox = new QCheckBox(tr("Match &case"),this);
    _pBackwardCheckBox = new QCheckBox(tr("Search &backward"),this);

    _pFindButton = new QPushButton(tr("&Find"));
    _pFindButton->setDefault(true);
    _pFindButton->setEnabled(false);

    _pCloseButton = new QPushButton(tr("Close"));

    // connection
    connect(_pLineEdit, SIGNAL(textChanged(const QString &)),this, SLOT(enableFindButton(const QString &)));
    connect(_pFindButton, SIGNAL(clicked()),                 this, SLOT(findClicked()));
    connect(_pCloseButton,SIGNAL(clicked()),                 this, SLOT(close()));

    // layout
    QHBoxLayout *pTopLeftLayout = new QHBoxLayout;
    pTopLeftLayout->addWidget(_pLabel);
    pTopLeftLayout->addWidget(_pLineEdit);

    QVBoxLayout *pLeftLayout = new QVBoxLayout;
    pLeftLayout->addLayout(pTopLeftLayout);
    pLeftLayout->addWidget(_pCaseCheckBox);
    pLeftLayout->addWidget(_pBackwardCheckBox);

    QVBoxLayout *pRightLayout = new QVBoxLayout;
    pRightLayout->addWidget(_pFindButton);
    pRightLayout->addWidget(_pCloseButton);
    pRightLayout->addStretch();

    QHBoxLayout *pMainLayout = new QHBoxLayout;
    pMainLayout->addLayout(pLeftLayout);
    pMainLayout->addLayout(pRightLayout);
    setLayout(pMainLayout);

    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
    setWindowIcon(QIcon(":/images/find.png"));
}

void CFindDialog::findClicked()
{
    QString sText = _pLineEdit->text();
    Qt::CaseSensitivity eCs = _pCaseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
    if (_pBackwardCheckBox->isChecked()) {
        emit findPrevious(sText, eCs);
    } else {
        emit findNext(sText, eCs);
    }
}

void CFindDialog::enableFindButton(const QString &sText_)
{
    _pFindButton->setEnabled(!sText_.isEmpty());
}
